package com.qianbei.common.init;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import com.qianbei.common.base.BaseApplication;

import java.util.UUID;

public class SystemConfig {

    private final static String SP_KEY = "HBSP";
    public String version;                          // 版本号
    public String os = "2";                         // 平台系统 默认：android 1苹果 2安卓
    public String device_name;                      //手机型号
    public String device_system_version;            //手机系统版本
    public String network_type="";                  //手机系统版本
    private String appID;                           // 客户端唯一标示
    private String cooke;

    private static SystemConfig sc;

    public SystemConfig() {
        getVersion();
        device_name = android.os.Build.MODEL.replace("[","").replace(" ","").replace("]","");
//        if(device_name.length()>6){
//            device_name= device_name.substring(0,6);
//        }
        device_system_version = Build.VERSION.SDK;
        network_type =GetNetworkType();
    }

    public static synchronized SystemConfig getInstance() {
        if (sc == null) {
            sc = new SystemConfig();
            // 配置默认值
        }

        return sc;
    }

    /**
     * 获取 APP版本号
     */
    private void getVersion() {
        try {
            version = BaseApplication.mApplication.getPackageManager()
                    .getPackageInfo(
                            BaseApplication.mApplication.getPackageName(), 0).versionName;
        } catch (NameNotFoundException e) {
            version = "";
        }
    }


    /**
     * 获取、生成、保存appID
     *
     * @return
     */
    private final static String KEY_APPID = "APPID";

    public String getAppID() {
        SharedPreferences sp = BaseApplication.mApplication.getSharedPreferences(
                SP_KEY, 0);
        if (appID != null || (appID = sp.getString(KEY_APPID, null)) != null) {
            return appID;
        }
        appID = UUID.randomUUID().toString();
        sp.edit().putString(KEY_APPID, appID).commit();
        return appID;
    }

    /*
     * 获取 cooke
     */
    private final static String KEY_COOKE = "HB_APPID";

    public String getCooke() {
        if (TextUtils.isEmpty(cooke)) {
            SharedPreferences sp = BaseApplication.mApplication.getSharedPreferences(
                    SP_KEY, 0);
            return sp.getString(KEY_COOKE, "");
        } else {
            return cooke;
        }
    }

    public void setCooke(String cookes) {

        if (cookes != null && !cookes.equals(cooke)) {
            SharedPreferences sp = BaseApplication.mApplication.getSharedPreferences(
                    SP_KEY, 0);
            sp.edit().putString(KEY_COOKE, cookes).commit();
            this.cooke = cookes;
        }


    }


    public static String GetNetworkType() {
        String strNetworkType = "";

        NetworkInfo networkInfo = ((ConnectivityManager) BaseApplication.mApplication.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                strNetworkType = "WIFI";
            } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                String _strSubTypeName = networkInfo.getSubtypeName();

                Log.e("cocos2d-x", "Network getSubtypeName : " + _strSubTypeName);

                // TD-SCDMA   networkType is 17
                int networkType = networkInfo.getSubtype();
                switch (networkType) {
                    case TelephonyManager.NETWORK_TYPE_GPRS:
                    case TelephonyManager.NETWORK_TYPE_EDGE:
                    case TelephonyManager.NETWORK_TYPE_CDMA:
                    case TelephonyManager.NETWORK_TYPE_1xRTT:
                    case TelephonyManager.NETWORK_TYPE_IDEN: //api<8 : replace by 11
                        strNetworkType = "2G";
                        break;
                    case TelephonyManager.NETWORK_TYPE_UMTS:
                    case TelephonyManager.NETWORK_TYPE_EVDO_0:
                    case TelephonyManager.NETWORK_TYPE_EVDO_A:
                    case TelephonyManager.NETWORK_TYPE_HSDPA:
                    case TelephonyManager.NETWORK_TYPE_HSUPA:
                    case TelephonyManager.NETWORK_TYPE_HSPA:
                    case TelephonyManager.NETWORK_TYPE_EVDO_B: //api<9 : replace by 14
                    case TelephonyManager.NETWORK_TYPE_EHRPD:  //api<11 : replace by 12
                    case TelephonyManager.NETWORK_TYPE_HSPAP:  //api<13 : replace by 15
                        strNetworkType = "3G";
                        break;
                    case TelephonyManager.NETWORK_TYPE_LTE:    //api<11 : replace by 13
                        strNetworkType = "4G";
                        break;
                    default:
                        // http://baike.baidu.com/item/TD-SCDMA 中国移动 联通 电信 三种3G制式
                        if (_strSubTypeName.equalsIgnoreCase("TD-SCDMA") || _strSubTypeName.equalsIgnoreCase("WCDMA") || _strSubTypeName.equalsIgnoreCase("CDMA2000")) {
                            strNetworkType = "3G";
                        } else {
                            strNetworkType = _strSubTypeName;
                        }

                        break;
                }

                Log.e("cocos2d-x", "Network getSubtype : " + Integer.valueOf(networkType).toString());
            }
        }

        Log.e("cocos2d-x", "Network Type : " + strNetworkType);

        return strNetworkType;
    }
}
